package com.crazy.util.lock;

/**
 * @author panyonghui
 * @describe 使用synchronized锁
 * @date 2022/1/11 9:47
 */
public class Demo1 {
    private static int num = 0;
    public static synchronized void addNum(){
        num ++;
    }

    static class Th extends Thread{
        @Override
        public void run() {
            for (int i = 0; i < 1000; i++){
                Demo1.addNum();
            }
        }
    }

    /**
     * @describe 创建三个线程对num执行1000预期结果为3000则执行正常
     * @auther crazy_cat
     * @date 2022/1/11 10:00
     * @param args
     * @return void
     * @modify
     */
    public static void main(String[] args) throws InterruptedException {
        Th t1 = new Th();
        Th t2 = new Th();
        Th t3 = new Th();
        t1.start();
        t2.start();
        t3.start();
        t1.join();
        t2.join();
        t3.join();
        System.out.println(Demo1.num);
    }
}
